package com.wavercaz.freeforum.services.impl;

import com.alibaba.fastjson2.JSON;
import com.wavercaz.freeforum.utils.ResponseResult;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Service
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(200);
        ResponseResult res = new ResponseResult(HttpStatus.FORBIDDEN.value(),"请登录");
        response.getWriter().print(JSON.toJSON(res));
    }
}
